package com.dyna.controller;

import com.dyna.bean.CommonsResult;
import com.dyna.bean.Payment;
import com.dyna.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class PayController {

    @Value("${server.port}")
    private String serverPort;

    @Autowired
    private PaymentService paymentService;

    @Resource
    private DiscoveryClient discoveryClient;

//    获取所有数据
    @RequestMapping("/")
    public CommonsResult getAll(){
        List<Payment> payments = paymentService.selectList();
        if (payments!=null){
            return new CommonsResult(200,"查询全部成功"+serverPort,payments);
        }else {
            return new CommonsResult(444,"查询全部失败",null);
        }
    }

//    通过id获取数据
    @RequestMapping("/byid/{id}")
    public CommonsResult getById(@PathVariable("id")Integer id){
        Payment byId = paymentService.getById(id);
        if (byId!=null){
            return new CommonsResult(200,"查询"+id+"成功"+serverPort,byId);
        }else {
            return new CommonsResult(444,"id查询失败",null);
        }
    }

//    添加数据
    @PostMapping("/add")
    public CommonsResult addpayment(@RequestBody Payment payment){
        Integer integer = paymentService.addPayment(payment);
        if (integer>0){
            return new CommonsResult(200,"添加成功",integer);
        }else {
            return new CommonsResult(444,"添加失败",null);
        }
    }
    @GetMapping("/payment8001/discover")
    public Object discover(){
//        获取该项目中的所有服务
        List<String> services = discoveryClient.getServices();
//        遍历所有的服务
        for (String service : services) {
//            将服务打印出来
            System.out.println("*****都有以下服务..."+service);
            List<ServiceInstance> instances = discoveryClient.getInstances(service);
            for (ServiceInstance ele : instances) {
                System.out.println(ele.getServiceId()+"\t"+
                        ele.getHost()+"\t"+
                        ele.getPort()+"\t"+
                        ele.getUri());
            }
        }
//        返回当前的discoverClient
        return this.discoveryClient;
    }

//    改写轮循
        @GetMapping(value = "/payment/lb")
        public String getPaymentLB(){
            return serverPort;
        }

        @ResponseBody
//    切记必须加上上面这个注解，会报下面这个错
//    status 404 reading FeignService#paymentFeignTimeOut()
        @GetMapping("/payment/feign/timeout")
        public String paymentFeignTimeOut(){
            try {
                System.out.println("端口号为："+serverPort);
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return serverPort;
        }

        @GetMapping("/payment/zipkin")
        public String paymentZipkin() {
            return "hi ,i'am paymentzipkin server fall back，welcome to atguigu，O(∩_∩)O哈哈~";
        }
}
